// pages/xinzeng/xinzeng.js
const db = wx.cloud.database()
const cont = db.collection('jihua');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    svg: ["cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/svg图标库/书法.svg",
      "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/svg图标库/书籍.svg",
      "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/svg图标库/人物.svg",
      "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/svg图标库/体育.svg",
      "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/svg图标库/写作.svg",
      "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/svg图标库/口红.svg",
      "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/svg图标库/太阳.svg",
      "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/svg图标库/工作.svg",
      "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/svg图标库/数学.svg",
      "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/svg图标库/洗澡.svg",
      "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/svg图标库/游戏.svg",
      "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/svg图标库/篮球.svg",
      "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/svg图标库/绘画.svg",
      "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/svg图标库/能力.svg",
      "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/svg图标库/英语.svg",
      "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/svg图标库/重要.svg",
      "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/svg图标库/钢琴老师-01.svg",
      "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/svg图标库/金牌.svg",
      "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/svg图标库/银牌.svg",
      "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/svg图标库/铜牌.svg",
    ],
    planValue: "",
    urgeValue: "",
    dayValue: "",
    svg1: "",
    trueday: 0,
    realityday: 0,
    continuationday: 0,
    time: "9:00",
    yundong: false,
    xuexi: false,
    shenghuo: false,
    color: "",
    leixing: "",
    id: "",
    flag: false,
    nowtime: "",
    mename: "",
    meimg: "",
    flagall:''

  },

  getplanValue: function (e) {
    var planValue = this.data.planValue
    this.setData({
      planValue: e.detail.value
    });
    console.log(planValue)
  },

  getdayValue: function (e) {
    var dayValuee = this.data.dayValue
    this.setData({
      dayValue: e.detail.value
    });
    console.log("day", dayValuee)
  },

  choosesvg: function (e) {
    // console.log("svg",e)
    console.log("kuaidiid", e.currentTarget.dataset.id)
    var id = e.currentTarget.dataset.id
    csvg = this.data.svg[id]
    this.setData({
      svg1: csvg
    })
    console.log(this.data.svg1)
    // wx.navigateTo({
    //   url: '/pages/kuaidi/kuaidi?id2=' +id,
    // })
  },

  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
    console.log(this.data.time)
  },

  geturgeValue: function (e) {
    var urgeValue = this.data.urgeValue
    this.setData({
      urgeValue: e.detail.value
    });
    console.log(urgeValue)
  },

  yundongclick: function () {
    if (this.data.yundong == false) {
      this.data.yundong = true
      this.data.xuexi = false
      this.data.shenghuo = false
      this.data.leixing = "运动"
      this.data.color = "#D6F4FF"
    }
    this.setData({
      yundong: this.data.yundong,
      xuexi: this.data.xuexi,
      shenghuo: this.data.shenghuo,
      leixing: this.data.leixing,
      color: this.data.color
    })
    console.log(this.data.yundong)
    console.log(this.data.leixing)
    console.log(this.data.color)
  },

  xuexiclick: function () {
    if (this.data.xuexi == false) {
      this.data.xuexi = true
      this.data.yundong = false
      this.data.shenghuo = false
      this.data.leixing = "学习"
      this.data.color = "#ffffff"
    }
    this.setData({
      yundong: this.data.yundong,
      xuexi: this.data.xuexi,
      shenghuo: this.data.shenghuo,
      leixing: this.data.leixing,
      color: this.data.color
    })
    console.log(this.data.xuexi)
    console.log(this.data.leixing)
    console.log(this.data.color)
  },

  shenghuoclick: function () {
    if (this.data.shenghuo == false) {
      this.data.shenghuo = true
      this.data.yundong = false
      this.data.xuexi = false
      this.data.leixing = "生活"
      this.data.color = "#FEFEDF"
    }
    this.setData({
      yundong: this.data.yundong,
      xuexi: this.data.xuexi,
      shenghuo: this.data.shenghuo,
      leixing: this.data.leixing,
      color: this.data.color
    })
    console.log(this.data.shenghuo)
    console.log(this.data.leixing)
    console.log(this.data.color)
  },

  switch: function () {
    if (this.data.flag == false) {
      this.data.flag = true
    } else {
      this.data.flag = false
    }
    this.setData({
      flag: this.data.flag
    })
    console.log(this.data.flag)
  },

  saveplan: function () {
    if (this.data.planValue == "" || this.data.dayValue == "None") {
      wx.showToast({
        title: '请输入计划名称',
        icon: 'none',
        duration: 2000
      })
      return;
    } else if (this.data.dayValue == "" || this.data.planValue == "None") {
      wx.showToast({
        title: '请输入计划天数',
        icon: 'none',
        duration: 2000
      })
      return;
    } else if (this.data.svg1 == "" || this.data.svg1 == "None") {
      wx.showToast({
        title: '请选择计划图标',
        icon: 'none',
        duration: 2000
      })
      return;
    } else if (this.data.yundong == "" && this.data.xuexi == "" && this.data.shenghuo == "") {
      wx.showToast({
        title: '请选择计划类型',
        icon: 'none',
        duration: 2000
      })
      return;
    } else if (this.data.urgeValue == "" || this.data.urgeValue == "None") {
      wx.showToast({
        title: '请输入激励语句',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    console.log('点击事件执行了')
    wx.getUserProfile({
      desc: '必须授权才能使用',
      success: res => {
        var mename = res.userInfo.nickName
        var meimg = res.userInfo.avatarUrl
        console.log(mename)
        console.log(meimg)
        console.log('成功', res)
        this.setData({
          mename: mename,
          meimg: meimg
        })
        db.collection('jihua').add({
          data: {
            plan: this.data.planValue,
            urge: this.data.urgeValue,
            day: this.data.dayValue,
            leixing: this.data.leixing,
            svg: this.data.svg1,
            time: this.data.time,
            color: this.data.color,
            flag: this.data.flag,
            trueday: this.data.trueday,
            nowtime: this.data.nowtime,
            finish: false,
            whenday: "",
            recycleday: "",
            realityday:this.data.realityday,
            userjudge:"",
            continuationday: this.data.continuationday,
            mename: this.data.mename,
            meimg: this.data.meimg,
            use: ""
          },
          success: function (res) {
            console.log(res._id)
            var id = res._id
            this.setData({
              id: id
            })
          }
        });
        setTimeout(function () {
          wx.switchTab({
            url: '/pages/shouye/shouye',
          })
        }, 1800);
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var D = new Date();
        if (D.getMonth() < 9) {
            taskStartTime = D.getFullYear() + "-0" + (D.getMonth() + 1) + "-"
        } else {
            taskStartTime = D.getFullYear() + "-" + (D.getMonth() + 1) + "-"
        }
        if (D.getDate() < 10) {
            taskStartTime += "0" + D.getDate()
        } else {
            taskStartTime += D.getDate()
        }
        console.log(taskStartTime)
    var time = taskStartTime;
    this.setData({
      nowtime: time
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    db.collection("jihua").doc("133e253361c098a2014d34b24c6bfc2d").get({
      success: res => {
        var flagall=res.data.flag
        this.setData({
          flagall:flagall
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})